package com.zzptc.myk.myapplication.ui.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.DialogTitle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.zzptc.myk.myapplication.Adapter.BookAdapter;
import com.zzptc.myk.myapplication.Adapter.CollectionAdapter;
import com.zzptc.myk.myapplication.Bean.Book;
import com.zzptc.myk.myapplication.Bean.ChooseBook;
import com.zzptc.myk.myapplication.Bean.CollectionItem;
import com.zzptc.myk.myapplication.R;
import com.zzptc.myk.myapplication.ReadActivity;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private ListView booklv;
    List<Book> bookList=new ArrayList<>();
    List<ChooseBook> chooseBooksList=new ArrayList<>();
    List<CollectionItem> collectionItemList=new ArrayList<>();
    CollectionAdapter adapter;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_home, container, false);
        initData();

        initView(root);
        setListener();
        return root;
    }

    private void initData() {
        SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        int uid=sp.getInt("userid",-1);
        chooseBooksList=LitePal.where("userid=?",uid+"").find(ChooseBook.class);
        bookList=LitePal.findAll(Book.class);
        for (Book b:bookList){
            Boolean k=false;
            for (ChooseBook c:chooseBooksList){
                if (b.getId()==c.getKookid()){
                    k=true;
                }
            }
            CollectionItem item=new CollectionItem(b.getBookimg(),b.getBookname(),b.getAuthorname(),k);
            collectionItemList.add(item);
        }
    }

    private void setListener() {
        booklv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it=new Intent(getContext(), ReadActivity.class);
                it.putExtra("bookname",collectionItemList.get(position).getBookname());
                startActivity(it);
            }
        });


        booklv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences sp=getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                int uid=sp.getInt("userid",-1);
                int bid=bookList.get(position).getId();
                if (ifChooseBook(uid,bid)){
                    new ChooseBook(0,uid,bid).save();
                    collectionItemList.get(position).setIscollected(true);;
                    adapter.notifyDataSetChanged();
                    showToast("收藏成功");
                }
                test();
                return true;
            }

            private void test() {
                for (ChooseBook c:LitePal.findAll(ChooseBook.class)){
                    Log.e("choosebook",c.getUserid()+"   "+c.getKookid());
                }

            }
        });
    }
    private boolean ifChooseBook(int uid,int bid){
        for (ChooseBook c: LitePal.findAll(ChooseBook.class)){
            if (c.getUserid()==uid&&c.getKookid()==bid){
                return false;
            }
        }
        return true;
    }


    private void initView(View root) {
        booklv=root.findViewById(R.id.booklv);
        bookList= LitePal.findAll(Book.class);
        adapter=new CollectionAdapter(getContext(),R.layout.collection_item,collectionItemList);
        booklv.setAdapter(adapter);
    }
    private void showToast(String str){
        Toast.makeText(getContext(),str,Toast.LENGTH_SHORT).show();
    }
}